package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TrFile;
import com.ruoyi.system.service.ITrFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 附件管理Controller
 * 
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/system/file")
public class TrFileController extends BaseController
{
    @Autowired
    private ITrFileService trFileService;

    /**
     * 查询附件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrFile trFile)
    {
        startPage();
        List<TrFile> list = trFileService.selectTrFileList(trFile);
        return getDataTable(list);
    }

    /**
     * 导出附件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "附件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrFile trFile)
    {
        List<TrFile> list = trFileService.selectTrFileList(trFile);
        ExcelUtil<TrFile> util = new ExcelUtil<TrFile>(TrFile.class);
        util.exportExcel(response, list, "附件管理数据");
    }

    /**
     * 获取附件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(trFileService.selectTrFileByFileId(fileId));
    }

    /**
     * 新增附件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "附件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrFile trFile)
    {

//        trFile.setFileName(trFile.getFileUrl().substring(trFile.getFileUrl().lastIndexOf("/")+1));
//        trFile.setFileType(trFile.getFileUrl().substring(trFile.getFileUrl().lastIndexOf(".")));
//



        return toAjax(trFileService.insertTrFile(trFile));
    }

    /**
     * 修改附件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "附件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrFile trFile)
    {
        return toAjax(trFileService.updateTrFile(trFile));
    }

    /**
     * 删除附件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "附件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(trFileService.deleteTrFileByFileIds(fileIds));
    }
}
